﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Windows.Forms;

namespace MailTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var MailMessage_Mai = new MailMessage();

                MailAddress MailAddress_to = null;// 设置收信人地址  不需要密码

                SmtpClient mail = new SmtpClient();
                //发送方式
                mail.DeliveryMethod = SmtpDeliveryMethod.Network;
                //初始化Ｓmtp服务器信息,
                mail.Host = tb_sendUrl.Text;
                //端口
                if (tb_sendDNS.Text != "")
                {
                    mail.Port = Convert.ToInt32(tb_sendDNS.Text);
                }
                //验证发件邮箱地址和密码
                mail.Credentials = new System.Net.NetworkCredential(tb_loginName.Text, tb_loginPwd.Text);

                string rcontentstr60 = tb_mailtile.Text;
                if (rcontentstr60 != "")
                {
                    //邮件主题
                    MailMessage_Mai.Subject = rcontentstr60;
                    MailMessage_Mai.SubjectEncoding = System.Text.Encoding.UTF8;
                }
                MailMessage_Mai.Headers.Add("X-Priority", "3");

                MailMessage_Mai.Headers.Add("X-MSMail-Priority", "Normal");

                MailMessage_Mai.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");   //本文以outlook名义发送邮件，不会被当作垃圾邮件             

                MailMessage_Mai.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");

                MailMessage_Mai.Headers.Add("ReturnReceipt", "1");    

                //邮件正文
                MailMessage_Mai.Body = tb_sendContent.Text;
                MailMessage_Mai.BodyEncoding = System.Text.Encoding.UTF8;
                //HTML编译
                MailMessage_Mai.IsBodyHtml = true;
                //级别
                MailMessage_Mai.Priority = MailPriority.High;
                //发件人邮箱
                MailMessage_Mai.From = new MailAddress(tb_sendMail.Text);


                string targetstr40 = tb_targUrl.Text;//收件人地址
                if (targetstr40 != "")
                {
                    MailAddress_to = new MailAddress(targetstr40);
                    MailMessage_Mai.To.Add(MailAddress_to);

                }

                mail.Send(MailMessage_Mai);



            }
            catch (Exception ex)
            {
                string result = ex.Message;
                //if (result.Contains("事务"))
                //{
                //    result = "被判定为垃圾邮件，未能发送成功！";
                //}
                MessageBox.Show(result);
            }
        }
    }
}
